using UnityEngine;
using UnityEngine.UI;

public class BombBall : PoolBall
{
	private float m_TimeRemain;

	public static Delegate1Args<IPlayer> GameoverWithBoom;

	[SerializeField]
	private GameObject m_BombEffect;

	[SerializeField]
	private float m_TimeToGameOver = 1f;

	private string m_PoolKey = "Bomb";

	private Text m_BombTickText;

	private RectTransform m_BombTickTextTransform;

	private Transform m_BombTrans;

	public override void Awake()
	{
		base.Awake();
		m_TimeRemain = 60f;
		m_BombTickText = GetComponentInChildren<Text>();
		m_BombTickText.transform.SetParent(GameManager.CurrentUIRoot.transform);
		m_BombTickText.transform.SetAsFirstSibling();
		m_BombTickTextTransform = (m_BombTickText.transform as RectTransform);
		m_BombTrans = base.transform;
	}

	public override void Start()
	{
		base.Start();
		SceneInitialization.Instance.InitObject(m_PoolKey, 1, m_BombEffect);
	}

	public override void Update()
	{
		if (GameManager.Rules.State != GlobalState.GAMEOVER && (m_state & (State.POTTED | State.HIDE)) == State.NONE)
		{
			base.Update();
			m_TimeRemain -= Time.deltaTime;
			if (m_TimeRemain < 0f)
			{
				m_TimeRemain = 0f;
			}
			m_BombTickText.text = ((int)m_TimeRemain).ToString();
			m_BombTickTextTransform.position = MathTools.World2UI(m_BombTrans.position);
			if (m_TimeRemain <= 0f)
			{
				Boom();
			}
		}
	}

	private void OnDestroy()
	{
		if ((bool)m_BombTickText)
		{
			UnityEngine.Object.Destroy(m_BombTickText.gameObject);
		}
	}

	public override void OpenRenderer()
	{
		base.OpenRenderer();
	}

	public override void CloseRenderer()
	{
		base.CloseRenderer();
		m_BombTickText.gameObject.SetActive(value: false);
	}

	private void Boom()
	{
		AudioManager.Explosion();
		SceneInitialization.Instance.GetObject(m_PoolKey).transform.position = base.transform.position;
		Invoke("GameOverWithBoom", m_TimeToGameOver);
		base.gameObject.SetActive(value: false);
		((PoolRulesMission)GameManager.Rules).RemoveBall(GetBallID());
	}

	private void GameOverWithBoom()
	{
		if (GameManager.Rules.State != GlobalState.GAMEOVER)
		{
			GameManager.Rules.State = GlobalState.GAMEOVER;
			if (GameoverWithBoom != null)
			{
				GameoverWithBoom(null);
			}
		}
		UnityEngine.Object.Destroy(base.gameObject);
	}
}
